package com.yay.warehouse;

import com.yay.warehouse.model.Category;
import com.yay.warehouse.model.LFType;
import com.yay.warehouse.model.Ticket;
import com.yay.warehouse.service.L10N;
import com.yay.warehouse.service.LocationService;
import com.yay.warehouse.service.TicketsService;
import com.yay.warehouse.tabs.TicketsAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


/**
 * Get Started!
 * @author yakovlev.yuriy@gmail.com
 * @author Dmitry Makeev
 */
public class Step3 extends Activity implements OnClickListener {
	private TextView description;
	private TextView tName;
	private TextView tCategory;
	private TextView tDescription;
	private TextView tAddress;
	private TextView tPhone;
	private ImageView lblRemove;
	private Button btnDone;
    private static TicketsService ticketsService;
    private String s3LDescription; 
	private String s3FDescription; 
    private String S = " ";
	
    
    @Override
	public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
         L10N.init(this.getResources());
		 setContentView(R.layout.step3);

		 final Ticket currentTicket = TicketsService.getCurrentTicket();
		 getTicketsService().save(currentTicket);
		 
	     switch(LFType.findById(currentTicket.getType())) {
		     case LOST: getDescription().setText(getS3LDescription());
		     			break;
		     default: 	getDescription().setText(getS3FDescription());
	     }
	    
	     Resources res = this.getResources();
		 if(currentTicket.getName() != null) getTName().setText(getTName().getText() + S + currentTicket.getName());
	     getTCategory().setText(getTCategory().getText() + S + res.getString(Category.findById(currentTicket.getCategory()).getName()));
	     if(currentTicket.getDescription() != null) getTDescription().setText(getTDescription().getText() + S + currentTicket.getDescription());
	     getTAddress().setText(getTAddress().getText() + S + LocationService.getAddress(getBaseContext(), currentTicket.getLatitude(), currentTicket.getLongitude()));
	     if(currentTicket.getContactPhone() != null) getTPhone().setText(getTPhone().getText() + S + currentTicket.getContactPhone());

	     initLblRemove();
	     initBtnDone();
	 }
	
	 @Override
	 public void onClick(View arg0) {
		 if(btnDone.equals(arg0)) {
			 Dashboard.reload = true;
			 finish();
		 } else if(lblRemove.equals(arg0)) {
			 Resources res = this.getResources();
			 AlertDialog.Builder alertbox = new AlertDialog.Builder(Step3.this);
             alertbox.setMessage(res.getString(R.string.areYouSure));
             alertbox.setPositiveButton(res.getString(R.string.yes), new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface arg0, int arg1) {
                 	// remove record
                 	getTicketsService().remove(TicketsService.getCurrentTicket());
                 	TicketsAdapter.reload = true;
                 	finish();
                 }
             });
             alertbox.setNegativeButton(res.getString(R.string.no), new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface arg0, int arg1) {
                 }
             });
             alertbox.show();
		 }
	 }
	 
	 
	// PRIVATE METHODS
	 
    private TicketsService getTicketsService() {
		if(ticketsService == null) {
			ticketsService = new TicketsService();
		}
		return ticketsService;
	}
    
	private String getS3LDescription() {
		if(s3LDescription == null) {
			s3LDescription = this.getResources().getString(R.string.s3_ldescription);
		}
		return s3LDescription;
	}
	
	private String getS3FDescription() {
		if(s3FDescription == null) {
			s3FDescription = this.getResources().getString(R.string.s3_fdescription);
		}
		return s3FDescription;
	}
    
	
	private TextView getDescription() {
		if(description == null) {
			description = (TextView)findViewById(R.id.description);
		    description.setTypeface(L10N.font);
		}
		return description;
	}
	
	private TextView getTName(){
		if(tName == null) {
			tName = (TextView)findViewById(R.id.lblName);
		}
		return tName;
	}

	private TextView getTCategory(){
		if(tCategory == null) {
			tCategory = (TextView)findViewById(R.id.lblCategory);
		}
		return tCategory;
	}
	
	private TextView getTDescription(){
		if(tDescription == null) {
			tDescription = (TextView)findViewById(R.id.lblDescription);
		}
		return tDescription;
	}
	
	private TextView getTAddress(){
		if(tAddress == null) {
			tAddress = (TextView)findViewById(R.id.lblAddress);
		}
		return tAddress;
	}
	
	private TextView getTPhone(){
		if(tPhone == null) {
			tPhone = (TextView)findViewById(R.id.lblContactPhone);
		}
		return tPhone;
	}
	
	private void initBtnDone() {
		if(btnDone == null) {
			btnDone = (Button) findViewById(R.id.btnDone);
		    btnDone.setTypeface(L10N.font);
		    btnDone.setOnClickListener(this);
		}
	}
	
	private void initLblRemove() {
		if(lblRemove == null) {
		    lblRemove = (ImageView)findViewById(R.id.lblRemove);
		    lblRemove.setOnClickListener(this);
		}
	}
	 
}
